package com.iss.yunpan.service.impl;

import com.iss.yunpan.dao.FileDao;
import com.iss.yunpan.entity.FileContent;
import com.iss.yunpan.entity.Users;
import com.iss.yunpan.service.FileService;
import com.obs.services.ObsClient;
import com.obs.services.model.ObsObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private HttpSession session;

    private String endPoint="obs.cn-east-3.myhuaweicloud.com";
    private String ak="N70O0XEWDEHGO4YA38CY";
    private String sk="oTZKNwTav6FBkRR0HBmUFamjHHrxF9qvYCU3SYOj";
    private String bucaketname="senventh";

    @Autowired
    private FileDao dao;
    @Override
    public List<FileContent> getAllFile(String fileName,String parentId){
        return dao.getAllFile(fileName,parentId);
    }

    @Override
    public void uploadFile(String fileName, InputStream input, String parentId) throws IOException {
        FileContent con=dao.getFileContentByFileNameAndParentId(fileName,Integer.valueOf(parentId));
        Users user = (Users) session.getAttribute("users");
        if(user!= null && !user.getUserId().equals("")){
            if(con!=null){
                con.setFileSize(input.available()+"kb");
                con.setUploadTime(new Date());
                dao.updateFile(con);
            }else {
                FileContent cont=new FileContent();
                cont.setFileName(fileName);
                cont.setFileSize(input.available()+"kb");
                cont.setFileAbsolute(fileName);
                cont.setFileType(fileName.substring(fileName.lastIndexOf("."),fileName.length()));
                cont.setParentId(Integer.valueOf(parentId));
                cont.setUserId(user.getUserId());
                cont.setUploadTime(new Date());
                dao.insertFile(cont);
            }
        }
    }

    @Override
    public boolean checkFileContent(String fileName, Integer parentId) {
        FileContent cont = dao.getFileContentByFileNameAndParentId(fileName,parentId);
        if(cont!=null){
            return true;
        }
        return false;
    }

    @Override
    public void insertFloder(String fileName, Integer parentId) {
        Users user = (Users) session.getAttribute("users");
        if(user!= null && !user.getUserId().equals("")){
            FileContent con= new FileContent();
            con.setFileName(fileName);
            con.setParentId(parentId);
            con.setUploadTime(new Date());
            con.setUserId(user.getUserId());
            con.setFileType("文件夹");
            con.setFileAbsolute(fileName);
            dao.insertFile(con);
            //obs创建实例
            ObsClient obsClient = new ObsClient(ak, sk, endPoint);
            //文件夹下创建实例
            obsClient.putObject("bucketname", fileName, new ByteArrayInputStream(new byte[0]));
        }
    }

    @Override
    public byte[] download(String fileAbsolute) throws IOException {
        try{
            ObsClient obsClient = new ObsClient(ak, sk, endPoint);
            ObsObject obsObject = obsClient.getObject(bucaketname, fileAbsolute);// 读取对象内容
            InputStream input = obsObject.getObjectContent();
            byte[] b = new byte[1024];
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int len;
            while ((len=input.read(b)) != -1){
                bos.write(b, 0, len);
            }
            byte[] out =  bos.toByteArray();
            bos.close();
            input.close();
            return out;
        }catch (IOException e){
            e.printStackTrace();
        }
        return null;
    }

}